package ca.uwo.android;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.widget.GridView;


/**
 * Parent class for any screen that needs a grid of icons e.g. Alumni and Mustangs.
 * @author tracyg
 *
 */
public abstract class HomeScreenActivity extends BaseMenuActivity {

	
	/**
	 * Alert dialog that gets shown on error
	 */
	protected AlertDialog alertDialog;
	
	
	/**
	 * Get list of icons
	 * @return a list of icon ids
	 */
	protected abstract Integer[] getModuleIcons();
	
	/**
	 * Get list of module names
	 * @return a list of module string ids
	 */
	protected abstract int getModuleNames();

	/**
	 * Get id of this grid view
	 * @return the id of the grid view
	 */
	protected abstract  int getGridViewId();
	
	/**
	 * Get id of content view
	 * @return id of content view
	 */
	protected abstract int getContentViewId();

	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(getContentViewId());

		GridView gridView = (GridView) findViewById(getGridViewId());
		gridView.setAdapter(new ImageAdapter(this, this.getResources()
				.getStringArray(getModuleNames()), getModuleIcons()));	

	}

	
	

	/**
	 * Check that a network connection is available If it is not, alert the
	 * user, and exit
	 * 
	 * @param context
	 */
	protected void verifyNetworkConnection(Context context) {
		// detect the state of the network connection and give user an error
		// message
		// with the option to exit if the network is not available
		ConnectivityManager connMgr = (ConnectivityManager) getApplicationContext()
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netInfo = connMgr.getActiveNetworkInfo();
		// Log.i(TAG_NAME, "Got netInfo, state = " + ((netInfo == null) ? "null"
		// : netInfo.getState()));
		if (netInfo == null
				|| netInfo.getState() != NetworkInfo.State.CONNECTED) {
			alert(
					"Internet connection required for this application to work properly.  Please connect, then try again.",
					true);
		}
	}

	/**
	 * Show alert dialog to the user
	 * 
	 * @param msg
	 *            the message to show the user
	 * @param exit
	 *            whether to exit after showing the dialog or continue
	 */
	protected void alert(String msg, final boolean exit) {
		alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setMessage(msg);
		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				if (exit)
					finish();

			}
		});
		alertDialog.show();
	}
	
	@Override
	public void onResume(){
		super.onResume();
		GridView gridView = (GridView) findViewById(getGridViewId());
		verifyNetworkConnection(gridView.getContext());
	}

	/**
	 * Start a news activity
	 * 
	 * @param url
	 *            the url of the RSS news feed
	 * @param header
	 *            the name that should be put in the screen header
	 */
	protected void startNewsActivity(String url, String header) {
		Intent intent = new Intent(this,
				ca.uwo.android.rss.news.RssNewsFeed.class);
		intent.putExtra("URL", url);
		intent.putExtra("HEADER", header);
		startActivity(intent);
	}
	
	protected void startEventModulePrototype(String url1, String url2, String url3, String url4, String url5){
				
		Intent intent = new Intent(this, ca.uwo.android.rss.events.RssEventsFeed.class);
		intent.putExtra("ACADEMIC_URL", url1);
		intent.putExtra("ALUMNI_URL", url2);
		intent.putExtra("MUSTANGS_URL", url3);
		intent.putExtra("SERVICE_URL", url4);
		intent.putExtra("WESTERN_URL", url5);
		startActivity(intent);
	}
	
	
	/**
	 * Start a date activity
	 * 
	 * @param url
	 *            the url of the RSS dates feed
	 * @param header
	 *            the name that should be put in the screen header
	 * @param calendarType
	 *            the type of calendar this is
	 */
	protected void startEventCalendarActivity(String url, String header,int calendarType) {
	
		Intent intent = new Intent(this,
				ca.uwo.android.rss.events.RssEventsFeed.class);
		intent.putExtra("URL", url);
		intent.putExtra("HEADER", header);
		intent.putExtra("TYPE", calendarType);
		startActivity(intent);
	}
	
	
	/**
	 * Start a web page activity.
	 * @param url 
	 * 			the url of the web page to be opened.
	 */
	protected void startWebPageActivity(String url){
		Intent intent = new Intent(this, ca.uwo.android.ViewWebPage.class);
		intent.putExtra("URL",url);
		startActivity(intent);
	}

}
